package com.masterclient.app.businessmodule.minemodule.address;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselib.enums.FCPageViewState;
import com.eoner.baselib.widget.empty.FCPageStateView;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.baselibrary.bean.address.AddressBean;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.middlelib.router.intercept.FCBaseIntercept;
import com.eoner.middlelib.router.map.FCRouterPath;
import com.masterclient.app.R;
import com.masterclient.app.base.FCBusinessActivity;
import com.masterclient.app.businessmodule.minemodule.address.adapter.AddressAdapter;
import com.masterclient.app.businessmodule.minemodule.address.contract.ChangeAddressContract;
import com.masterclient.app.businessmodule.minemodule.address.contract.ChangeAddressPresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者：sean
 * 创建时间：2017/7/28 17:08
 * 功能说明：
 */

public class ChangeAddressActivity extends FCBusinessActivity<ChangeAddressPresenter> implements ChangeAddressContract.View{

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_add_addre)
    TextView tvAddAddress;
    @BindView(R.id.empty_view)
    FCPageStateView emptyView;
    private AddressAdapter addressAdapter;
    private int mPosition;
    private String isPurchase = "0";

    @Override
    protected int requireGetLayoutId() {
        return R.layout.activity_change_address;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        navigationBar.setTitle("收货地址").setRightBtnText("管理").setRightBtnTextSize(13).setRightBtnTextColor(Color.parseColor("#666666"));
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {
        //判断是否是赠送品
        isPurchase  = getIntent().getStringExtra("isPurchase");
        mPosition = getIntent().getIntExtra("position", -1);

        addressAdapter = new AddressAdapter(isPurchase);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(addressAdapter);
        addressAdapter.bindToRecyclerView(recyclerView);
        emptyView.setViewState(FCPageViewState.PAGE_STATE_NO_DATA,R.mipmap.icon_empty_address,"留个地址，说不定有惊喜");
        TextView emptyBtn = emptyView.findViewById(R.id.tv_base_state_refresh);
        emptyBtn.setText("添加地址");
        emptyBtn.setVisibility(View.VISIBLE);
        emptyBtn.setOnClickListener(v->{
            startActivitry(AddNewAddressActivity.class,new String[][]{{"isPurchase", isPurchase}});
        });
        emptyView.setVisibility(View.GONE);

        addressAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.rl_con:
                        AddressBean.DataBean.ShItemsBean address = mPresenter.list.get(position);
                        CheckBox cb = (CheckBox) view.findViewById(R.id.cb_check);
                        if(!cb.isChecked()){
                            cb.setChecked(true);
                        }

                        Intent intent = new Intent();
                        intent.putExtra("default_address",address);
                        intent.putExtra("position", mPosition);
                        setResult(103,intent);
                        finish();
//                        mPresenter.setAddressDefault(list.get(position).getSh_id());
                        break;
                }
            }
        });
    }

    @Override
    protected void onResume() {
        mPresenter.getAddressList();
        super.onResume();
    }

//    @Override
//    public void finish() {
//        Intent intent = new Intent();
//        for(AddressBean.DataBean.ShItemsBean itemsBean:list){
//            if("1".equals(itemsBean.getSh_is_default())){
//                intent.putExtra("default_address",itemsBean);
//                break;
//            }
//        }
//        setResult(103,intent);
//        super.finish();
//    }


    @Override
    protected void onNavigationRightClick() {
        super.onNavigationRightClick();
        ARouter.getInstance().build(FCRouterPath.MINE_ADDRESS_PATH).withString("isPurchase",isPurchase).navigation(this,new FCBaseIntercept());
    }

    @OnClick({R.id.tv_add_addre}) void onClick(View view){
        switch (view.getId()){
            case R.id.tv_add_addre:
                startActivitry(AddNewAddressActivity.class,new String[][]{{"isPurchase",isPurchase}});
                break;
        }
    }

    @Override
    public void getAddressSuccess(AddressBean.DataBean dataBean) {
        this.mPresenter.list  =dataBean.getSh_items();
        addressAdapter.setNewData(dataBean.getSh_items());
        if (mPresenter.list != null && mPresenter.list.size() > 0) {
            emptyView.setVisibility(View.GONE);
        } else {
            emptyView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void changeDefaultSuccess() {
        showToast("设置默认地址成功");
        mPresenter.getAddressList();
    }

    @Override
    public void changeDefaultfailed() {
        showToast("设置默认地址失败");
        mPresenter.getAddressList();
    }

    @Override
    public void baseShowError() {
        super.baseShowError();
        emptyView.setVisibility(View.GONE);
    }

    @Override
    public FCPageStateView baseRefreshPageState(int viewState) {
        FCPageStateView view = super.baseRefreshPageState(viewState);
        baseRefreshPageStateBottomOffset(ScreenUtils.dp2px(80));
        return view;
    }

    @Override
    public void onClickPageStateErrorView() {
        super.onClickPageStateErrorView();
        mPresenter.getAddressList();
    }

}
